#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include"BlockQueue.hpp"
#include"Task.hpp"

void *Consumer(void*args)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(args);
    int count = 1;
    while (true)
    {
        sleep(1);
        //1.获取数据
        Task data;
        bq->Pop(&data);
        printf("%d.Consumer -> %d + %d = %d\n",count++ ,data._a, data._b, data._result);
    }
}
void*Producer(void*args)
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(args);
    int count = 1;
    while (true)
    {
        sleep(2);
        int a = rand() % 10 + 1, b = rand() % 10 + 2;
        Task data(a,b);
        bq->Equeue(data);
        printf("%d.Producer -> %d + %d = ?\n",count++, a, b);
    }
}
int main()
{
    BlockQueue<int> bq(5);
    pthread_t thread_1, thread_2;
    int n = pthread_create(&thread_1, nullptr, Producer, &bq);
    if(n!=0)
    {
        perror("pthread_create");
    }
    n = pthread_create(&thread_1, nullptr, Consumer, &bq);
    if(n!=0)
    {
        perror("pthread_create");
    }

    pthread_join(thread_1, nullptr);
    pthread_join(thread_2, nullptr);
}